//package com.bestcem.xm.txtanls.config;
//
//import org.springframework.beans.factory.ObjectProvider;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration;
//import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorProperties;
//import org.springframework.boot.actuate.health.CompositeHealthContributor;
//import org.springframework.boot.actuate.health.HealthContributor;
//import org.springframework.boot.actuate.health.NamedContributor;
//import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;
//import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
//import org.springframework.util.Assert;
//
//import javax.sql.DataSource;
//import java.util.Iterator;
//import java.util.Map;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
///**
// * ShardingSphere不适用原来的健康检查语句 isValid,改为自定义sql语句
// *
// * @author zhangfeng <feng.zhang@idiaoyan.com>
// * @version v1.0
// * @date 2021/9/13 16:20
// */
//@Configuration
//public class DataSourceHealthConfig extends DataSourceHealthContributorAutoConfiguration {
//    @Value("${spring.datasource.shardingSphere.validation-query:select 1}")
//    private String defaultQuery;
//
//    public DataSourceHealthConfig(ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
//        super(metadataProviders);
//    }
//
//    @Override
//    public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources,
//                                                 DataSourceHealthIndicatorProperties dataSourceHealthIndicatorProperties) {
//        if (dataSourceHealthIndicatorProperties.isIgnoreRoutingDataSources()) {
//            Map<String, DataSource> filteredDatasources = dataSources.entrySet().stream()
//                    .filter((e) -> !(e.getValue() instanceof AbstractRoutingDataSource))
//                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
//            return createContributor(filteredDatasources);
//        }
//        return createContributor(dataSources);
//    }
//
//    private HealthContributor createContributor(Map<String, DataSource> beans) {
//        Assert.notEmpty(beans, "Beans must not be empty");
//        if (beans.size() == 1) {
//            return createContributor(beans.values().iterator().next());
//        }
//        return CompositeHealthContributor.fromMap(beans, this::createContributor);
//    }
//
//    private HealthContributor createContributor(DataSource source) {
//        if (source instanceof AbstractRoutingDataSource) {
//            AbstractRoutingDataSource routingDataSource = (AbstractRoutingDataSource) source;
//            return new RoutingDataSourceHealthContributor(routingDataSource, this::createContributor);
//        }
//        return new DataSourceHealthIndicator(source, getValidationQuery(source));
//    }
//
//    private String getValidationQuery(DataSource source) {
//        return defaultQuery;
//    }
//
//    /**
//     * {@link CompositeHealthContributor} used for {@link AbstractRoutingDataSource} beans
//     * where the overall health is composed of a {@link DataSourceHealthIndicator} for
//     * each routed datasource.
//     */
//    static class RoutingDataSourceHealthContributor implements CompositeHealthContributor {
//
//        private final CompositeHealthContributor delegate;
//
//        RoutingDataSourceHealthContributor(AbstractRoutingDataSource routingDataSource,
//                                           Function<DataSource, HealthContributor> contributorFunction) {
//            Map<String, DataSource> routedDataSources = routingDataSource.getResolvedDataSources().entrySet().stream()
//                    .collect(Collectors.toMap((e) -> e.getKey().toString(), Map.Entry::getValue));
//            this.delegate = CompositeHealthContributor.fromMap(routedDataSources, contributorFunction);
//        }
//
//        @Override
//        public HealthContributor getContributor(String name) {
//            return this.delegate.getContributor(name);
//        }
//
//        @Override
//        public Iterator<NamedContributor<HealthContributor>> iterator() {
//            return this.delegate.iterator();
//        }
//
//    }
//}
